package com.lepu.app.fun.chat.bean;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.database.Cursor;

import com.app.utils.LoginUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lepu.app.fun.chat.util.DbQueryRunner;

public class BeanDoctorNotice {

	public String DoctorID;// 医生ID int
	public String DoctorName;// 医生名字 string
	public String NoticeID;// 通知ID int
	public String Content;// 通知内容 string
	public String RecordCreateTime;// 创建记录时间 string
	public String DoctorAvatar;// 医生头像 string

	public static ArrayList<BeanDoctorNotice> parseListInfo(String dataString) {
		String result = "";
		try {
			JSONObject jsonObject = new JSONObject(dataString);
			JSONObject obj1 = (JSONObject) jsonObject.get("Result");
			JSONArray jsonArray = obj1.getJSONArray("ListInfo");

			result = jsonArray.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		Gson gson = new Gson();
		ArrayList<BeanDoctorNotice> dataList = gson.fromJson(result,
				new TypeToken<List<BeanDoctorNotice>>() {
				}.getType());
		return dataList;
	}

	public static void saveNoticeDb(List<BeanDoctorNotice> notices) {

        DbQueryRunner<?> queryRunner = DbQueryRunner.getInstance();

        if (null != notices && 0 < notices.size()) {
            try {
                queryRunner.beginTransaction();
                BeanDoctorNotice notice = null;
                for (int i = 0; i < notices.size(); i++) {
                    notice = notices.get(i);
                    String sql = "REPLACE into Notice (Content, DoctorID, NoticeID, RecordCreateTime, UniqueID, " +
                            "HostID, DoctorName,SynTime) " +
                            " values(?,?,?,?,?,?,?,?)";

                    Object params[] = {notice.Content, notice.DoctorID, notice.NoticeID, notice.RecordCreateTime, notice.DoctorAvatar,
                            LoginUtil.getUserId(), notice.DoctorName, ""};
                    queryRunner.update(sql, params);
                }
                queryRunner.setTransactionSuccessful();
            } catch (Exception e) {
            } finally {
                queryRunner.endTransaction();
            }
        }
    }

	public static List<BeanDoctorNotice> getNoticeListItem(String hostId) {
		DbQueryRunner<?> queryRunner = DbQueryRunner.getInstance();
		String sql = "select * from Notice where HostID=? order by RecordCreateTime desc limit 70";
		String[] params = new String[] { hostId };
		@SuppressWarnings({ "rawtypes", "unchecked" })
		List<BeanDoctorNotice> list = queryRunner.query(sql,
				new DbQueryRunner.RowHandler() {
					@Override
					public Object handle(Cursor c) {
						BeanDoctorNotice notice = new BeanDoctorNotice();
						notice.Content = c.getString(c
								.getColumnIndex("Content"));
						notice.DoctorID = c.getString(c
								.getColumnIndex("DoctorID"));
						notice.DoctorName = c.getString(c
								.getColumnIndex("DoctorName"));
						notice.NoticeID = c.getString(c
								.getColumnIndex("NoticeID"));
						notice.RecordCreateTime = c.getString(c
								.getColumnIndex("RecordCreateTime"));
						notice.DoctorAvatar = c.getString(c
								.getColumnIndex("UniqueID"));
						return notice;
					}

				}, params);
		return list;
	}
}
